#include "connection.h"
#include "indexframeworksessionservice.h"
#include "fileevent.pb.h"
#include "sessionfileeventmonitor.h"

#include <QCoreApplication>
#include <QThread>

#include <QCommandLineParser>
#include <QCommandLineOption>

#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>

#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
    if (getuid() != 0) {
        qCritical() << "session service must setup by host";
        return 0;
    }
    quint32 uid = 0;
    int readfd = 0;
    int writefd = 0;
    uid =  QString(argv[1]).toUInt();
    readfd = QString(argv[2]).toInt();
    writefd = QString(argv[3]).toInt();

    int fd[2];
    fd[0] = readfd;
    fd[1] = writefd;

    int ret = setuid(uid);
    if (ret != 0) {
        int errsv = errno;
        qCritical() << "set uid error:" << qt_error_string(errsv) << ", return" << ret;
    }

    QCoreApplication a(argc, argv);

    Connection conn(uid, fd);
    IndexFrameworkSessionService service(&conn, &a);

    qDebug() << "connect to user session service" << getuid();
    auto connection = QDBusConnection::sessionBus();
    if (!connection.isConnected()) {
        qCritical() << "failed to connect session bus" << ", error" << connection.lastError();
        return 0;
    }
    if (!connection.registerService("org.ukui.IndexFrameworkSessionService")) {
        qCritical() << "failed to register service" << connection.lastError();
        return 0;
    }
    if (!connection.registerObject("/", "org.ukui.IndexFrameworkSessionService", &service, QDBusConnection::ExportAllSignals|QDBusConnection::ExportAllSlots)) {
        qCritical() << "failed to register service object" << connection.lastError();
        return 0;
    }

    QThread t;
    SessionFileEventMonitor monitor;
    monitor.moveToThread(&t);

    QObject::connect(&t, &QThread::started, &monitor, &SessionFileEventMonitor::setupService);
    QObject::connect(&service, &IndexFrameworkSessionService::messageReceived, &monitor, &SessionFileEventMonitor::handleFileEventProtobuf);
    t.start();

//    const char *typeName[] = {"FileCreate", "FileDelete", "FileRename", "FileContentChange", "FilePermissionChange"};
//    QObject::connect(&service, &IndexFrameworkSessionService::messageReceived, &a, [typeName](const QByteArray &buffer){
//        FileEvent event;
//        event.ParseFromArray(buffer.constData(), buffer.size());
//        qDebug() << "===========recieve protobuf message===========" <<
//                    "pid:" << event.pid() << "command:" << QString::fromStdString(event.command()) << "type:" << typeName[event.eventtype()] << "\n" <<
//                    "src dev:" << event.srcdev() << "src uuid:" << QString::fromStdString(event.srcuuid()) << "src id:" << event.srcino() << "src fullpath:" << QString::fromStdString(event.srcpath()) << "\n" <<
//                    "dst dev:" << event.dstdev() << "dst uuid:" << QString::fromStdString(event.dstuuid()) << "dst id:" << event.dstino() << "dst fullpath:" << QString::fromStdString(event.dstpath());
//    });

    return a.exec();
}
